package org.jeecg.modules.thresholdPlan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.thresholdPlan.entity.ThresholdPlan;
import org.jeecg.modules.thresholdPlan.entity.ThresholdPlanDetail;
import org.jeecg.modules.thresholdPlan.mapper.ThresholdPlanDetailMapper;
import org.jeecg.modules.thresholdPlan.mapper.ThresholdPlanMapper;
import org.jeecg.modules.thresholdPlan.service.IThresholdPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 温度阈值方案
 * @Author: jeecg-boot
 * @Date:   2023-11-22
 * @Version: V1.0
 */
@Service
public class ThresholdPlanServiceImpl extends ServiceImpl<ThresholdPlanMapper, ThresholdPlan> implements IThresholdPlanService {

	@Autowired
	private ThresholdPlanMapper thresholdPlanMapper;
	@Autowired
	private ThresholdPlanDetailMapper thresholdPlanDetailMapper;

	@Override
	public void updateEnable(ThresholdPlan thresholdPlan) {
		if ("启用".equals(thresholdPlan.getEnable())){
			ThresholdPlan plan = new ThresholdPlan();
			plan.setEnable("禁用");
			thresholdPlanMapper.update(plan,null);
		}
	}

	@Override
	public List<ThresholdPlanDetail> getEnableThreshold() {
		LambdaQueryWrapper<ThresholdPlan> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(ThresholdPlan::getEnable, "启用");
		ThresholdPlan thresholdPlan = thresholdPlanMapper.selectOne(wrapper);
		return thresholdPlanDetailMapper.selectByMainId(thresholdPlan.getId());
	}

	@Override
	@Transactional
	public void saveMain(ThresholdPlan thresholdPlan, List<ThresholdPlanDetail> thresholdPlanDetailList) {
		thresholdPlanMapper.insert(thresholdPlan);
		if(thresholdPlanDetailList!=null && thresholdPlanDetailList.size()>0) {
			for(ThresholdPlanDetail entity:thresholdPlanDetailList) {
				//外键设置
				entity.setPlanId(thresholdPlan.getId());
				thresholdPlanDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ThresholdPlan thresholdPlan,List<ThresholdPlanDetail> thresholdPlanDetailList) {
		thresholdPlanMapper.updateById(thresholdPlan);
		
		//1.先删除子表数据
		thresholdPlanDetailMapper.deleteByMainId(thresholdPlan.getId());
		
		//2.子表数据重新插入
		if(thresholdPlanDetailList!=null && thresholdPlanDetailList.size()>0) {
			for(ThresholdPlanDetail entity:thresholdPlanDetailList) {
				//外键设置
				entity.setPlanId(thresholdPlan.getId());
				thresholdPlanDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		thresholdPlanDetailMapper.deleteByMainId(id);
		thresholdPlanMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			thresholdPlanDetailMapper.deleteByMainId(id.toString());
			thresholdPlanMapper.deleteById(id);
		}
	}
	
}
